<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form ref="query" :model="page.query" inline label-position="right" label-width="80px">
        <el-form-item label="模具名称" prop="moldName">
          <el-input v-model="page.query.moldName" placeholder="请输入模具名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <!--      <div>
        <span>模具信息列表</span>
      </div>
     <el-divider />-->
      <div class="table-button">
        <el-button type="primary" class="el-icon-plus" @click="saveClick">新增</el-button>
        <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">批量删除</el-button>
        <el-button plain type="primary" class="el-icon-upload2" @click="importClick">导入</el-button>
        <download-button :disabled="tableData.length===0" :params="downloadParam" :path="downloadApiPath" icon="el-icon-download" plain />
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
      </div>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45" />
        <el-table-column label="模具名称" prop="moldName" />
        <el-table-column label="分类" prop="categoryName" />
        <el-table-column label="单位" prop="unit" />
        <el-table-column label="供应商" prop="supplierName" />
        <el-table-column label="制造日期" prop="madeDate" />
        <el-table-column label="产能" prop="capacity">
          <template slot-scope="scope">
            {{ scope.row.capacity }}
          </template>
        </el-table-column>
        <el-table-column label="重量(克)" prop="weight" />
        <el-table-column label="位置" prop="position" />
        <el-table-column label="备注" prop="remark" show-overflow-tooltip />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="updateClick(scope.row)"
            >修改
            </el-button>
            <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='取消'
              placement="top"
              style="margin-left: 10px"
              title="是否删除此条数据"
              @confirm="handleDelConfirm(scope.row)"
            >
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <div v-if="dialogVisible">
      <submit-module ref="submitModule" :visible="dialogVisible" @close="handleClose" />
    </div>
    <div v-if="importDialogVisible">
      <custom-upload
        :visible="importDialogVisible"
        :download-template-api-path="downloadTemplatePath"
        :upload-api-path="uploadApiPath"
        @close="closeImportDialog"
      />
    </div>
  </div>
</template>

<script>
import { deleteMold, selectMoldPage } from '@/api/erp/mold'
import SubmitModule from '@/views/erp/mold/modules/submitModule.vue'

export default {
  name: 'Index',
  components: { SubmitModule },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      downloadApiPath: '/api/erp/mold/export',
      uploadApiPath: '/api/erp/mold/import',
      downloadTemplatePath: '/api/erp/mold/template',
      downloadParam: { fileName: '模具信息清单' },
      dialogVisible: false,
      importDialogVisible: false,
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          moldName: '',
          state: ''
        }
      }
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      selectMoldPage(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.doPage()
    },
    // 新增按钮点击
    saveClick() {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init()
      })
    },
    // 修改按钮点击
    updateClick(row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(row.id)
      })
    },
    // 批量删除点击
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.moldId) }
        deleteMold(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
            this.doPage()
          }
        }).finally(() => {
          this.tableLoading = false
        })
      })
    },
    // 导入点击
    importClick() {
      this.importDialogVisible = true
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    // 表单关闭
    handleClose() {
      this.dialogVisible = false
      this.doPage()
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      deleteMold(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    closeImportDialog() {
      this.importDialogVisible = false
      this.doPage()
    }
  }
}
</script>

<style scoped>

</style>
